﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using agile_tool.userControl;

namespace agile_tool
{
    /// <summary>
    /// Interaction logic for userStoryPage.xaml
    /// </summary>
    public partial class userStoryPage : UserControl, IGenericPagesData
    {
        Button createNewuserStory;
        create createuserStoryItem;
        int projectId;
        public string navigationName = "";
        public userStoryPage(int projectId, string filter,string projectName)
        {
            InitializeComponent();
            this.projectId = projectId;
            navigationName = projectName + " User Story";
            refresh(filter);

        }
        
        public string getNavigationName()
        {
            return navigationName;
        }
        public int getId()
        {
            return projectId;
        }
        public void refresh(string filter)
        {
            lbUserStorys.Items.Clear();
            
            try
            {

                DataClasses1DataContext db = new DataClasses1DataContext();
                var userStorys = from p in db.tblUserStories where p.projectId == projectId select p;
               

                foreach (var p in userStorys)
                {
                    if (filter == "")
                    {
                        userStoryItem theuserStoryItem = new userStoryItem();
                        theuserStoryItem.tbName.Text = p.name;
                        theuserStoryItem.tbDesc.Text = p.description;
                        theuserStoryItem.tbPriority.Text = p.Priority.ToString();
                        theuserStoryItem.tbEffort.Text = p.effort.ToString();
                        theuserStoryItem.id = p.id;
                        theuserStoryItem.btnDell.Click += new RoutedEventHandler(btnDell_Click);
                        theuserStoryItem.btnEdit.Click += new RoutedEventHandler(btnEdit_Click);
                        theuserStoryItem.btnAcceptanceCrtiteria.Click += new RoutedEventHandler(btnAcceptanceCrtiteria_Click);
                        theuserStoryItem.btnTask.Click += new RoutedEventHandler(btnTask_Click);
                        lbUserStorys.Items.Add(theuserStoryItem);
                    }
                    else
                    {
                        if (p.name.IndexOf(filter) != -1)
                        {
                            userStoryItem theuserStoryItem = new userStoryItem();
                            theuserStoryItem.tbName.Text = p.name;
                            theuserStoryItem.tbDesc.Text = p.description;
                            theuserStoryItem.tbPriority.Text = p.Priority.ToString();
                            theuserStoryItem.tbEffort.Text = p.effort.ToString();
                            theuserStoryItem.id = p.id;
                            theuserStoryItem.btnDell.Click += new RoutedEventHandler(btnDell_Click);
                            theuserStoryItem.btnEdit.Click += new RoutedEventHandler(btnEdit_Click);

                            lbUserStorys.Items.Add(theuserStoryItem);
                        }
                    }

                }
            }
            catch { }
            createNewuserStory = new Button { Content = "Create New User Story" };
            createNewuserStory.Click += new RoutedEventHandler(createNewuserStory_Click);
            createNewuserStory.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            lbUserStorys.Items.Add(createNewuserStory);
        }

        void btnTask_Click(object sender, RoutedEventArgs e)
        {
            userStoryItem theUserStoryItemItem = new userStoryItem();
            theUserStoryItemItem = (userStoryItem)helper.FindParrentUserControl((Button)sender, theUserStoryItemItem.GetType().ToString());

            taskPage theTaskPage = new taskPage(theUserStoryItemItem.id, ((MainWindow)MainWindow.stateList[0]).tbSeatch.Text, theUserStoryItemItem.tbName.Text);
            MainWindow.stateList.Add(theTaskPage);
            ((MainWindow)MainWindow.stateList[0]).spBody.Children.Add(theTaskPage);
            ((MainWindow)MainWindow.stateList[0]).refreshNavButton();
            this.Visibility = Visibility.Collapsed;
        }

        void btnAcceptanceCrtiteria_Click(object sender, RoutedEventArgs e)
        {
            userStoryItem theUserStoryItemItem = new userStoryItem();
            theUserStoryItemItem = (userStoryItem)helper.FindParrentUserControl((Button)sender, theUserStoryItemItem.GetType().ToString());

            acceptanceCriteriaPage theAcceptanceCriteriaPage = new acceptanceCriteriaPage(theUserStoryItemItem.id, ((MainWindow)MainWindow.stateList[0]).tbSeatch.Text, theUserStoryItemItem.tbName.Text);
            MainWindow.stateList.Add(theAcceptanceCriteriaPage);
            ((MainWindow)MainWindow.stateList[0]).spBody.Children.Add(theAcceptanceCriteriaPage);
            ((MainWindow)MainWindow.stateList[0]).refreshNavButton();
            this.Visibility = Visibility.Collapsed;
        }

    
       
        void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            userStoryItem theuserStoryItem =new userStoryItem();
            theuserStoryItem = (userStoryItem)helper.FindParrentUserControl((Button)sender, theuserStoryItem.GetType().ToString());
            if (theuserStoryItem.spSlide.Height == 0)
            {
                theuserStoryItem.openTheDesc();
                theuserStoryItem.btnEdit.Content = "Save";
            }
            else
            {
                if (theuserStoryItem.tbName.Text == "")
                { MessageBox.Show("The Name Can't be Empty"); }
                else
                {
                    theuserStoryItem.closeTheDesc();
                    theuserStoryItem.btnEdit.Content = "Edit";
                    DataClasses1DataContext db = new DataClasses1DataContext();
                    tblUserStory tb = (from p in db.tblUserStories where p.id == theuserStoryItem.id select p).First();
                    tb.name = theuserStoryItem.tbName.Text;
                    tb.description = theuserStoryItem.tbDesc.Text;
                    tb.Priority = int.Parse(theuserStoryItem.tbPriority.Text);
                    tb.effort = int.Parse(theuserStoryItem.tbEffort.Text);
                    db.SubmitChanges();
                }
            }
        }

        public static Boolean deleteUserStory(int id)
        {

            DataClasses1DataContext db = new DataClasses1DataContext();
                
            try
            {
                var tbUserSp = from p in db.tblUserStorySprints where p.userStoryId == id select p;
                tbUserSp.First();
                MessageBox.Show("You Can't Delete User Story Which is Assigned to Sprint");
                return false;
                }
                catch { 
                var tsakHour = from p in db.tblTaskHours where p.tblTask.userStoryId == id select p;
                db.tblTaskHours.DeleteAllOnSubmit(tsakHour);
                db.SubmitChanges();

                var tbtas = from p in db.tblTasks where p.userStoryId == id select p;
                db.tblTasks.DeleteAllOnSubmit(tbtas);
                db.SubmitChanges();


                var tbac = from p in db.tblAcceptanceCriterias where p.userStoryId == id select p;
                db.tblAcceptanceCriterias.DeleteAllOnSubmit(tbac);
                db.SubmitChanges();
               

                tblUserStory tb = (from p in db.tblUserStories where p.id == id select p).First();
                db.tblUserStories.DeleteOnSubmit(tb);
                db.SubmitChanges();
                return true;
             }
        }

        void btnDell_Click(object sender, RoutedEventArgs e)
        {
            userStoryItem theuserStoryItem = new userStoryItem();
            theuserStoryItem = (userStoryItem)helper.FindParrentUserControl(((Button)sender), theuserStoryItem.GetType().ToString());

            if (deleteUserStory(theuserStoryItem.id))
            {
                lbUserStorys.Items.Remove(theuserStoryItem);
            }

        }

        void createNewuserStory_Click(object sender, RoutedEventArgs e)
        {
            lbUserStorys.Items.Remove(createNewuserStory);
            createuserStoryItem = new create();
            createuserStoryItem.btnCancel.Click += new RoutedEventHandler(btnCancel_Click);
            createuserStoryItem.btnSave.Click += new RoutedEventHandler(btnSave_Click);

            lbUserStorys.Items.Add(createuserStoryItem);
        }

        void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (createuserStoryItem.tbName.Text != "")
            {
                DataClasses1DataContext db = new DataClasses1DataContext();
                tblUserStory tp = new tblUserStory { name = createuserStoryItem.tbName.Text, description = createuserStoryItem.tbDesc.Text, projectId = projectId  };
                db.tblUserStories.InsertOnSubmit(tp);
                db.SubmitChanges();

                refresh("");
            }
            else
            {
                MessageBox.Show("You need a name for your userStory");
            }
        }

        void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            refresh("");
        }
    }
}
